﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace Pcubed.Shuie.ContextProvider
{
  class ContextProvider
  {
    public CurrentListContext CurrentList { get; set; }
    public List<FieldContext> Fields { get; set; }
    public String FormMode { get; set; }
    public Boolean IsDesignTime { get; set; }
    public Int32 ItemId { get; set; }
    public List<ListContext> Lists { get; set; }
    public SiteContext Site { get; set; }
    public WebContext Web { get; set; }
    public List<ContentType> ContentTypes { get; set; }

    public static ContextProvider GetContext()
    {
      ContextProvider context =
        new ContextProvider
        {
          FormMode = SPContext.Current.FormContext.FormMode.ToString(),
          IsDesignTime = SPContext.Current.IsDesignTime,
          ItemId = SPContext.Current.ItemId,
          CurrentList = (null == SPContext.Current.List) ?
            null :
            new CurrentListContext
            {
              AllowRssFeeds = SPContext.Current.List.AllowRssFeeds,
              BaseTemplate = (Int32)SPContext.Current.List.BaseTemplate,
              BaseType = SPContext.Current.List.BaseType,
              CanReceiveEmail = SPContext.Current.List.CanReceiveEmail,
              ContentTypes = SPContext.Current.List.ContentTypes,
              ContentTypesEnabled = SPContext.Current.List.ContentTypesEnabled,
              Created = SPContext.Current.List.Created,
              DefaultContentApprovalWorkflowId =
                SPContext.Current.List.DefaultContentApprovalWorkflowId,
              DefaultItemOpen = SPContext.Current.List.DefaultItemOpen,
              DefaultViewID = SPContext.Current.List.DefaultView.ID,
              DefaultViewImageUrl = SPContext.Current.List.DefaultView.ImageUrl,
              DefaultViewTitle = SPContext.Current.List.DefaultView.Title,
              DefaultViewUrl = SPContext.Current.List.DefaultViewUrl,
              Description = SPContext.Current.List.Description.Normalize(),
              Direction = SPContext.Current.List.Direction,
              EmailAlias = SPContext.Current.List.EmailAlias,
              EnableVersioning = SPContext.Current.List.EnableVersioning,
              Folders = SPContext.Current.List.Folders,
              ForceCheckout = SPContext.Current.List.ForceCheckout,
              ID = SPContext.Current.List.ID,
              ImageUrl = SPContext.Current.List.ImageUrl,
              ItemCount = SPContext.Current.List.ItemCount,
              LastItemModifiedDate = SPContext.Current.List.LastItemModifiedDate,
              MobileDefaultViewUrl = SPContext.Current.List.MobileDefaultViewUrl,
              OnQuickLaunch = SPContext.Current.List.OnQuickLaunch,
              Ordered = SPContext.Current.List.Ordered,
              TemplateFeatureId = SPContext.Current.List.TemplateFeatureId,
              Title = SPContext.Current.List.Title.Normalize(),
              Version = SPContext.Current.List.Version
            }
        };

      if (null != context.CurrentList && null != SPContext.Current.Fields)
      {
        context.Fields = new List<FieldContext>();
        foreach (SPField spField in SPContext.Current.Fields)
        {
          FieldContext field =
            new FieldContext
            {
              AuthoringInfo = spField.AuthoringInfo,
              CanToggleHidden = spField.CanToggleHidden,
              DefaultValue = spField.DefaultValue,
              Description = spField.Description.Normalize(),
              Direction = spField.Direction,
              DisplaySize = spField.DisplaySize,
              FieldReferences = spField.FieldReferences,
              Filterable = spField.Filterable,
              FromBaseType = spField.FromBaseType,
              Id = spField.Id,
              InternalName = spField.InternalName,
              ReadOnlyField = spField.ReadOnlyField,
              Reorderable = spField.Reorderable,
              Required = spField.Required,
              Scope = spField.Scope,
              StaticName = spField.StaticName,
              Title = spField.Title.Normalize(),
              TypeAsString = spField.TypeAsString
            };

          context.Fields.Add(field);
        }
      }

      if (null != SPContext.Current.Web.Lists)
      {
        context.Lists = new List<ListContext>();
        foreach (SPList spList in SPContext.Current.Web.Lists)
        {
          ListContext list =
            new ListContext
            {
              BaseTemplate = (Int32)spList.BaseTemplate,
              BaseType = spList.BaseType,
              DefaultViewUrl = spList.DefaultViewUrl,
              Description = spList.Description.Normalize(),
              Hidden = spList.Hidden,
              ID = spList.ID,
              ImageUrl = spList.ImageUrl,
              ItemCount = spList.ItemCount,
              OnQuickLaunch = spList.OnQuickLaunch,
              Title = spList.Title.Normalize()
            };
        }
      }

      if (null != context.CurrentList && context.CurrentList.ContentTypesEnabled)
      {
        context.ContentTypes = new List<ContentType>();
        foreach (SPContentType spContentType in SPContext.Current.List.ContentTypes)
        {
          ContentType contentType =
              new ContentType
              {
                Description = spContentType.Description,
                Name = spContentType.Name,
                Id = spContentType.Id.ToString(),
                DocumentTemplate = spContentType.DocumentTemplate.ToString(),
                DocumentTemplateURL = spContentType.DocumentTemplateUrl
              };

          context.ContentTypes.Add(contentType);
        }
      }

      if (null != SPContext.Current.Web.Lists)
      {
        context.Lists = new List<ListContext>();
        foreach (SPList spList in SPContext.Current.Web.Lists)
        {
          ListContext list =
            new ListContext
            {
              BaseTemplate = (Int32)spList.BaseTemplate,
              BaseType = spList.BaseType,
              DefaultViewUrl = spList.DefaultViewUrl,
              Description = spList.Description.Normalize(),
              Hidden = spList.Hidden,
              ID = spList.ID,
              ImageUrl = spList.ImageUrl,
              ItemCount = spList.ItemCount,
              OnQuickLaunch = spList.OnQuickLaunch,
              Title = spList.Title.Normalize()
            };

          if (null != spList.DefaultView)
          {
            list.DefaultViewID = spList.DefaultView.ID;
            list.DefaultViewImageUrl = spList.DefaultView.ImageUrl;
            list.DefaultViewTitle = spList.DefaultView.Title;
          }

          context.Lists.Add(list);
        }
      }

      context.Site =
        new SiteContext
        {
          HostName = SPContext.Current.Site.HostName,
          ID = SPContext.Current.Site.ID,
          LastContentModifiedDate = SPContext.Current.Site.LastContentModifiedDate,
          Port = SPContext.Current.Site.Port,
          PortalName = SPContext.Current.Site.PortalName,
          PortalUrl = SPContext.Current.Site.PortalUrl,
          Protocol = SPContext.Current.Site.Protocol,
          ServerRelativeUrl = SPContext.Current.Site.ServerRelativeUrl,
          Url = SPContext.Current.Site.Url
        };

      context.Web =
        new WebContext
        {
          /* to avoid dumping all ShUIE entries in p3context,
           * if we're in the web of the site collection, we don't
           * show the property bag
           */
          AllProperties = (!SPContext.Current.Web.Url.Equals(SPContext.Current.Site.Url)) ?
                        SPContext.Current.Web.AllProperties :
                        null,
          Created = SPContext.Current.Web.Created,
          CurrencyLocaleID = SPContext.Current.Web.CurrencyLocaleID,
          ID = SPContext.Current.Web.ID,
          IsRootWeb = SPContext.Current.Web.IsRootWeb,
          Language = SPContext.Current.Web.Language,
          LastItemModifiedDate = SPContext.Current.Web.LastItemModifiedDate,
          Locale = SPContext.Current.Web.Locale,
          Name = SPContext.Current.Web.Name,
          Navigation = SPContext.Current.Web.Navigation,
          PublicFolderRootUrl = SPContext.Current.Web.PublicFolderRootUrl,
          QuickLaunchEnabled = SPContext.Current.Web.QuickLaunchEnabled,
          RegionalSettings = SPContext.Current.Web.RegionalSettings,
          ServerRelativeUrl = SPContext.Current.Web.ServerRelativeUrl,
          Title = SPContext.Current.Web.Title.Normalize(),
          Url = SPContext.Current.Web.Url,
          UserIsSiteAdmin = SPContext.Current.Web.UserIsSiteAdmin,
          UserIsWebAdmin = SPContext.Current.Web.UserIsWebAdmin,
          WebTemplate = SPContext.Current.Web.WebTemplate,
          WebTemplateId = SPContext.Current.Web.WebTemplateId
        };

      return context;
    }

    public ShuieContext GetShuieContext()
    {
      return new ShuieContext()
      {
        SiteUri = Site.Url,
        WebUri = Web.Url,
        WebTitle = Web.Title,
        ListTitle = CurrentList != null ? CurrentList.Title.Normalize() : String.Empty,
        BaseTemplate = CurrentList != null ? CurrentList.BaseTemplate.ToString() : String.Empty,
        IsInvalidMode = "invalid".Equals(FormMode.ToLowerInvariant()),
        IsNewMode = "new".Equals(FormMode.ToLowerInvariant()),
        IsEditMode = "edit".Equals(FormMode.ToLowerInvariant()),
        IsDisplayMode = "display".Equals(FormMode.ToLowerInvariant())
      };
    }
  }
}